from fastapi import APIRouter, Request

from applications.configs.func import render_template
from applications.configs.response import ResponseMessage
from applications.schemas.V2ray import AddV2rayUsers
from applications.services.v2ray import synchronous_users, get_v2ray_user_list, add_users

v2ray_router = APIRouter(prefix="/v2ray", tags=["v2ray管理"])


@v2ray_router.get("/", summary='v2ray首页', description="v2ray首页")
def index(request: Request):
    return render_template(request, "/v2ray/index.html", data={"data": get_v2ray_user_list()})


@v2ray_router.post("/synchronous", summary='同步用户', description="同步用户")
def synchronous_user():
    code, msg = synchronous_users()
    return ResponseMessage(code, msg)


@v2ray_router.post("/add", summary='添加v2用户', description="添加v2用户")
def add_user(data: AddV2rayUsers):
    code, msg = add_users(data)
    return ResponseMessage(code, msg)